package com.zzl.bonds.ticket.constant.status;

import com.zzl.bonds.ticket.dto.KeyValue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 申请单的票据状态
 * 开单
 * 确认中：
 * 1 内部复核中
 * 2 平台审批中
 * 3 收单企业签收中
 * 失败：
 * 14 收单企业驳回
 * 12 内部复核未通过
 * 13 平台审批未通过
 * 17 已撤销
 * 成功：
 * 4 已签收
 *
 * @author : gull
 * @date : 2022/2/16 15:06
 */
public enum TicketStatusEnum {

    /**
     * 开单待复核
     */
    START(0, "开始", "开始", 0),
    OPEN_PENDING_REVIEW(1, "内部复核中", "内部复核中", 25),
    OPEN_PENDING_APPROVE(2, "平台审批中", "平台审批中", 50),
    OPEN_PENDING_SIGN(3, "收单企业签收中", "收单企业签收中", 75),
    NORMAL_HOLDING(4, "已签收", "已签收", 100),
    /**
     * 自己企业 复核未通过
     */
    REVIEW_REJECT(12, "内部复核未通过", "内部复核未通过", 25),
    /**
     * 平台审批未通过
     */
    APPROVE_REJECT(13, "平台审批未通过", "平台审批未通过", 50),
    /**
     * 供应商 拒绝签收
     */
    REFUSE(14, "收单企业驳回", "收单企业驳回", 75),
    CANCEL(17, "已撤销", "已撤销", 25),

    TRANSFER_PENDING_REVIEW(5, "内部复核中", "内部复核中", 25),
    TRANSFER_PENDING_APPROVE(6, "平台审批中", "平台审批中", 50),
    TRANSFER_PENDING_SIGN(7, "收单企业签收中", "收单企业签收中", 75),
    TRANSFER_SUCCESS(8, "已被拆转", "已签收", 100),
    /**
     * 融单状态
     */

    FINANCE_PENDING_REVIEW(9, "内部复核中", "待复核", 20),
    FINANCE_PENDING_APPROVE(10, "平台审批中", "待审批", 40),
    FINANCE_PENDING_SIGN(19, "融单签收中", "待签署", 60),
    FINANCE_PENDING_LOAN(20, "融单待放款", "待放款", 80),

    FINANCE_SUCCESS(11, "已被拆融", "已放款", 100),


    INVALID(18, "已失效", "已失效", null),


    WAITING_PAYMENT(15, "等待兑付", "", null),
    PAID(16, "已经兑付", "", null),

    ;
    private Integer code;

    private String name;
    /**
     * handsInfo 勿修改
     */
    private String description;
    /**
     * handProgress 处理进度,如25%，则progress=25
     */
    private Integer progress;
    /**
     * 失败
     */
    public static final List<Integer> REJECT_LIST = Arrays.asList(12, 13, 14, 18);
    /**
     * 转让中
     */
    public static final List<Integer> TRANSFER_IN_LIST = Arrays.asList(5, 6, 7);
    /**
     * 成功
     */
    public static final List<Integer> SUCCESS_IN_LIST = Collections.singletonList(8);

    /**
     * 融资中 票据状态
     */
    public static final List<Integer> FINANCE_ING_LIST = Arrays.asList(9, 10, 19, 20);
    /**
     * 融资成功
     */
    public static final List<Integer> FINANCE_SUCCESS_LIST = Collections.singletonList(11);


    /**
     * 开立中
     */
    public static final List<Integer> OPEN_ING_LIST = Arrays.asList(1, 2, 3);


    TicketStatusEnum(Integer code, String name, String description, Integer progress) {
        this.code = code;
        this.name = name;
        this.description = description;
        this.progress = progress;
    }

    /**
     * 获取融资处理状态枚举
     *
     * @param handStatus 处理状态
     * @return 审批状态集合
     */
    public static List<Integer> getFinanceHandStatusList(Integer handStatus) {
        //查全部的时候，状态字段不传代表查所有
        if (handStatus.equals(HandStatusEnum.ALL.getCode())) {
            return new ArrayList<>();
        } else if (handStatus.equals(HandStatusEnum.SUCCESS.getCode())) {
            return Arrays.asList(FINANCE_SUCCESS.code, WAITING_PAYMENT.code, PAID.code);
        } else if (handStatus.equals(HandStatusEnum.FAIL.getCode())) {
            return TicketStatusEnum.REJECT_LIST;
        } else {
            return TicketStatusEnum.FINANCE_ING_LIST;
        }
    }

    /**
     * 通过处理状态 获取审批状态
     *
     * @param handStatus 处理状态
     * @return 审批状态集合
     */
    public static List<Integer> getHandStatusList(Integer handStatus) {
        if (handStatus.equals(HandStatusEnum.ALL.getCode())) {
            return Arrays.stream(TicketStatusEnum.values()).map(x -> x.code).collect(Collectors.toList());
        } else if (handStatus.equals(HandStatusEnum.SUCCESS.getCode())) {
            return Arrays.asList(NORMAL_HOLDING.code, WAITING_PAYMENT.code, PAID.code);
        } else if (handStatus.equals(HandStatusEnum.FAIL.getCode())) {
            return Arrays.asList(REVIEW_REJECT.code, APPROVE_REJECT.code, REFUSE.code, CANCEL.code);
        } else {
            // 开立中
            return OPEN_ING_LIST;
        }
    }

    /**
     * 开立 可以撤回的  票据状态
     */
    public static List<Integer> CANCEL_LIST = Arrays.asList(OPEN_PENDING_REVIEW.code, OPEN_PENDING_APPROVE.code, OPEN_PENDING_SIGN.code);

    /**
     * 通过code获取枚举对象
     *
     * @param code code值
     * @return 枚举对象
     */
    public static String getStatusName(Integer code) {
        for (TicketStatusEnum value : TicketStatusEnum.values()) {
            if (value.code.equals(code)) {
                return value.name;
            }
        }
        return null;
    }

    /**
     * 根据状态获取进度
     *
     * @param code 状态值
     * @return 进度
     */
    public static Integer getProgress(Integer code) {
        for (TicketStatusEnum value : TicketStatusEnum.values()) {
            if (value.code.equals(code)) {
                return value.progress;
            }
        }
        return null;
    }

    /**
     * 通过code获取枚举对象
     *
     * @param code code值
     * @return 枚举对象
     */
    public static String getStatusHandInfo(Integer code) {
        for (TicketStatusEnum value : TicketStatusEnum.values()) {
            if (value.code.equals(code)) {
                return value.description;
            }
        }
        return null;
    }

    public static List<KeyValue> getTicketStatusList() {
        List<KeyValue> list = new ArrayList<>();
        for (TicketStatusEnum value : TicketStatusEnum.values()) {
            KeyValue keyValue = new KeyValue();
            keyValue.setKey(value.code);
            keyValue.setValue(value.name);
            list.add(keyValue);
        }
        return list;
    }

    /**
     * 通过code获取枚举对象
     *
     * @param code code值
     * @return 枚举对象
     */
    public static TicketStatusEnum getStatus(Integer code) {
        for (TicketStatusEnum value : TicketStatusEnum.values()) {
            if (value.code.equals(code)) {
                return value;
            }
        }
        return null;
    }


    public Integer getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }
}
